------------------------------------
-- these functions are misc ones needed
-- for the gamemode.
------------------------------------

------------------------------------
-- Stun the player 
------------------------------------
function StunPlayer(pl, duration)
	if(pl.StunDuration == nil) then
		pl.StunDuration = 0
	end
	pl.Stunned = true
	pl.StunDuration = pl.StunDuration+duration
	if pl.StunDuration>70 then pl.StunDuration=70 end
	timer.Create(pl:UniqueID() .. "STUN" , .2, pl.StunDuration, CheckStun, pl)
	pl:ConCommand("pp_motionblur 1")
	pl:ConCommand("pp_motionblur_addalpha 0.1")
	pl:ConCommand("pp_motionblur_delay 0.035")
	local blindamount = pl.StunDuration*0.025
	if blindamount>1 then blindamount = 1 end
	pl:ConCommand("pp_motionblur_drawalpha " .. (blindamount))
end

function CheckStun(pl)
	pl.StunDuration = pl.StunDuration-1
	if pl:GetNWBool("Antidoted") then
		pl.StunDuration = pl.StunDuration-3
	end
	local blindamount = pl.StunDuration*0.035
	if blindamount>1 then blindamount = 1 end
	pl:ConCommand("pp_motionblur 1")
	pl:ConCommand("pp_motionblur_drawalpha " .. (blindamount))
	if (pl.StunDuration>25) then
		pl:ViewPunch(Angle((pl.StunDuration-25)*((math.random()*0.2)-0.1), (pl.StunDuration-25)*((math.random()*0.2)-0.1), (pl.StunDuration-25)*((math.random()*0.1)-0.05)))
	end
	if (pl.StunDuration<=0) then
		UnStun(pl)
		return
	end
end

function UnStun(pl)
	pl.Stunned = false
	pl.StunDuration = 0
	timer.Destroy(pl:UniqueID() .. "STUN")
	pl:ConCommand("pp_motionblur 0")
end
hook.Add("DoPlayerDeath", "PlyDeathStun", UnStun)
hook.Add("PlayerDisconnected", "PlyDisconnectedStun", function(pl) timer.Destroy(pl:UniqueID() .. "STUN") end)

------------------------------------
-- Burn the player
------------------------------------
function BurnPlayer(pl, duration, attacker, inflictor)
	if ValidEntity(pl) then
		pl:SetNWBool("burned", true)
		pl.BurnAttacker = attacker
		pl.BurnInflictor = inflictor
		if pl.BurnDuration==nil then pl.BurnDuration=0 end
		pl.BurnDuration = pl.BurnDuration+duration
		if pl.BurnDuration>20 then pl.BurnDuration=20 end
		if pl.BurnDuration>=4 && !pl:IsOnFire() then
			pl:Ignite(pl.BurnDuration,0)
		end
		timer.Create(pl:UniqueID() .. "BURN" , 1, pl.BurnDuration, CheckBurn, pl)
	end
end

function CheckBurn(pl)
	pl.BurnDuration = pl.BurnDuration-1
	// lol.
	if pl:GetNWBool("Antidoted") then
		pl.BurnDuration = pl.BurnDuration-3
	end
	if pl:WaterLevel()>0 then
		pl.BurnDuration=pl.BurnDuration-pl:WaterLevel()
	end
	if pl.BurnDuration<=2 && pl:IsOnFire() then
		pl:Extinguish()
	end
	pl:TakeDamage(math.ceil(pl.BurnDuration/3), pl.BurnAttacker, pl.BurnInflictor)
	if (pl.BurnDuration<=0) then
		UnBurn(pl)
		return
	end
end

function UnBurn(pl)
	pl:Extinguish()
	pl:SetNWBool("burned", false)
	pl.BurnDuration = 0
	timer.Destroy(pl:UniqueID() .. "BURN")
end
hook.Add("DoPlayerDeath", "PlyDeathBurn", UnBurn)
hook.Add("PlayerDisconnected", "PlyDisconnectedBurn", function(pl) timer.Destroy(pl:UniqueID() .. "BURN") end)

------------------------------------
-- Poison the player
------------------------------------
function PoisonPlayer(pl, duration, attacker, inflictor)
	if ValidEntity(pl) && pl:IsPlayer() then
		pl:SetNWBool("poisoned", true)
		pl.Poisoned = true
		pl.PoisonAttacker = attacker
		pl.PoisonInflictor = inflictor
		if pl.PoisonDuration==nil then pl.PoisonDuration=0 end
		pl.PoisonDuration = pl.PoisonDuration+duration
		if pl.PoisonDuration>60 then pl.PoisonDuration=60 end
		pl:TakeDamage(math.ceil(pl.PoisonDuration/8), pl.PoisonAttacker, pl.PoisonInflictor)
		timer.Create(pl:UniqueID() .. "POISON" , 1, pl.PoisonDuration, CheckPoison, pl)
	end
end

function CheckPoison(pl)
	pl.PoisonDuration = pl.PoisonDuration-1
	// lol.
	if pl:GetNWBool("Antidoted") then
		pl.PoisonDuration = pl.PoisonDuration-5
	end
	pl:TakeDamage(math.ceil(pl.PoisonDuration/8), pl.PoisonAttacker, pl.PoisonInflictor)
	//pl:ViewPunch(Angle((pl.PoisonDuration-25)*((math.random()*0.2)-0.1), (pl.PoisonDuration-25)*((math.random()*0.2)-0.1), (pl.PoisonDuration-25)*((math.random()*0.1)-0.05)))
	if (pl.PoisonDuration<=0) then
		UnPoison(pl)
		return
	end
end
	
function UnPoison(pl)
	pl:SetNWBool("poisoned", false)
	pl.Poisoned = false
	pl.PoisonDuration = 0
	timer.Destroy(pl:UniqueID() .. "POISON")
end
hook.Add("DoPlayerDeath", "PlyDeathPoison", UnPoison)
hook.Add("PlayerDisconnected", "PlyDisconnectedPoison", function(pl) timer.Destroy(pl:UniqueID() .. "POISON") end)

------------------------------------
-- IsAllied(player1, player2)
-- checks if two players are mutually allied.
------------------------------------
function IsAllied(ply1, ply2)
	if(ValidEntity(ply1) || ValidEntity(ply2)) then return end
	
	if tonumber(ply1:GetInfo("rpdm_ally_"..ply2:UniqueID()))==1 && tonumber(ply2:GetInfo("rpdm_ally_"..ply1:UniqueID()))==1 then
		return true
	else
		return false
	end
end

function Purify ( strng )
	if (string.find(tostring(strng), [[%"]])) then
		strng = " "
	end
	if (string.find(tostring(strng), [[\n]])) then
		strng = " "
	end
	return strng
end


function Notify(ply, msgtype, len, msg)
	ply:PrintMessage(2, msg)
	umsg.Start("RPDMNotify", ply)
		umsg.String(msg)
		umsg.Short(msgtype)
		umsg.Short(len)
	umsg.End()
end

function NotifyAll(msgtype, len, msg)
	for k, v in pairs(player.GetAll()) do	
		Notify(v, msgtype, len, msg)	
	end

end

function PrintMessageAll(msgtype, msg)
	for k, v in pairs(player.GetAll()) do	
		v:PrintMessage(msgtype, msg)
	end

end

function FindPlayer(info)
	if info == nil then return nil end
	for k, v in pairs(player.GetAll()) do
		
		if(tonumber(info) == v:EntIndex()) then
			return v
		end
		
		if(info == v:SteamID()) then
			return v
		end
		
		if(string.find(string.lower(v:Nick()), string.lower(info)) != nil) then
			return v
		end
		
	end
	
	return nil

end

function ShockWaveExplosion(pos, ply, hitnorm, rad)
	rad = math.Round(rad)
	local efdt = EffectData()
		efdt:SetStart(pos)
		efdt:SetOrigin(pos)
		efdt:SetScale(1)
		efdt:SetRadius(rad)
		efdt:SetNormal(hitnorm)
	util.Effect("cball_bounce",efdt)
end

local uberexists=false
function UberDrugExists()
	if !uberexists then
		NotifyAll(1,5,"Someone has created an UberDrug!")
		uberexists=true
	end
end

function Disableunfreeze(weapon,player)
     return false
end
hook.Add("OnPhysgunReload", "DisableMingebags", Disableunfreeze)

function WeldControl(ent,ply)
	if (ValidEntity(ply)) then
		if ValidEntity(ent) then
			ent:SetNWInt("welddamage", 255)
		end
		ply.shitweldcount=ply.shitweldcount-1
		if (ply.shitweldcount<=0) then
			ply.shitweldcount=0
			ply.shitwelding = false
		end
	end
end

local meta = FindMetaTable("Entity")
function meta:IsDoor()

	local class = self:GetClass()
	
	if(class == "func_door" or
		class == "func_door_rotating" or
		class == "prop_door_rotating") then
		
		return true
		
	end
	
	return false

end